package com.hhhn.wk.main.tab2.view.xiangqing.fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.hhhn.wk.R;
import com.hhhn.wk.utils.Constants;

/**
 * A simple {@link Fragment} subclass.
 */
public class H5Fragment extends Fragment {
    private ProgressBar pg1;
    private WebView webView;
    String goods_id="";
    WebSettings settings;
    public H5Fragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view=inflater.inflate(R.layout.fragment_h5, container, false);
        savedInstanceState=getArguments();
        goods_id = savedInstanceState.getString("goods_id");

        webView =view.findViewById(R.id.webView);
        pg1=view.findViewById(R.id.progressBar1);
        initdata();
        return view;
    }

    private void initdata() {
         settings = webView.getSettings();
        //设置WebView属性,运行执行js脚本
        webView.getSettings().setJavaScriptEnabled(true);

        //支持屏幕缩放
        settings.setSupportZoom(true);
        settings.setBuiltInZoomControls(true);
        //不显示webview缩放按钮
        settings.setDisplayZoomControls(false);

//        webView.setFocusable(false);
        webView.loadUrl(Constants.GOODSdetails+goods_id);
        Log.d("链接id", "url: "+Constants.GOODSdetails+goods_id);
//        settings = webView.getSettings();
//
//        settings.setLoadWithOverviewMode(true);
//        settings.setBuiltInZoomControls(true);
//        settings.setLoadsImagesAutomatically(true);
//        settings.setRenderPriority(WebSettings.RenderPriority.HIGH);
//        settings.setBlockNetworkImage(true);
//        settings.setUseWideViewPort(true);
//        settings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
//        webView.setWebViewClient(new GoodsDetailWebViewClient());






        webView.setWebViewClient(new WebViewClient() {
            //设置在webView点击打开的新网页在当前界面显示,而不跳转到新的浏览器中
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });
        webView.setWebChromeClient(new WebChromeClient(){
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                if(newProgress==100){
                    //加载完网页进度条消失
                    pg1.setVisibility(View.GONE);
                }
                else{
                    //开始加载网页时显示进度条
                    pg1.setVisibility(View.VISIBLE);
                    //设置进度值
                    pg1.setProgress(newProgress);
                }
            }
        });

    }

    private class GoodsDetailWebViewClient extends WebViewClient {
        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            settings.setBlockNetworkImage(false);
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            return true;
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (webView != null) {
            webView.removeAllViews();
            webView.destroy();
            webView = null;
        }
    }
}
